<?php
/**
 * Created by PhpStorm.
 * User: tsy
 * Date: 2017/5/26
 * Time: 16:21
 */
namespace Admin\Model;
use Think\Model;
class CustCreditModel extends Model{
    protected $trueTableName = 'bypal_cust_zhimascore';

    public function select_zmxy_score($cust_id){
        $sql="select zmscore_score,zmscore_num from bypal_cust_zhimascore where cust_id='".$cust_id."' and zmscore_mark='1' order by zmscore_num asc";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function select_shzx_score($cust_id){
        $sql="select vpn_annex,state from bypal_cust_vpn where cust_id='$cust_id' order by create_time desc limit 0 ,1";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values[0];
    }

    public function select_fm_table($borrow_id){
        $sql="select decision,rules_name,score from
        bypal_impl_fm t1 left join bypal_impl_fmrules t2 on t1.fm_id=t2.fm_id where t1.code=$borrow_id";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function select_zmxy_table($cust_id,$num){
        $sql="select t1.zmrule_code,t1.zmrule_rule,t2.zmlevel_level,FROM_UNIXTIME(t1.update_time,'%Y-%m-%d') update_time
            from bypal_cust_zhimarule t1
            left join bypal_cust_zhimalevel t2 on t1.zmrule_code=t2.zmlevel_code
            where cust_id='$cust_id' and zmrule_mark='1' and zmrule_num=".$num;
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function select_max_num($cust_id){
        $sql="select max(zmscore_num) num from bypal_cust_zhimascore where cust_id='".$cust_id."'";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values[0]['num'];
    }

    public function insert_zhima_score($cust_id,$ivs_score,$num){
        $time=time();
        $sql="insert into bypal_cust_zhimascore (cust_id,zmscore_score,zmscore_num,update_time) values ('$cust_id','$ivs_score','$num','$time')";
        $Model= new \Think\Model();
        $values=$Model->execute($sql);
        return $values;
    }

    public function insert_zhima_rules($zhima_array,$cust_id,$num){
        $time=time();
        $count=count($zhima_array);
        $sql="insert into bypal_cust_zhimarule (cust_id,zmrule_code,zmrule_rule,zmrule_num,update_time) values ";
        if($count>0) {
            for ($i=0;$i<$count;$i++){
                $code = $zhima_array[$i]['code'];
                $rule = $zhima_array[$i]['description'];
                $sql.="('$cust_id','$code','$rule','$num','$time'),";
            }
            $sql=substr($sql,0,strlen($sql)-1);
            $Model= new \Think\Model();
            $values=$Model->execute($sql);
            return $values;
        }else{
            return 0;
        }
    }

    public function delete_zmxy_score($cust_id,$num){
        $time=time();
        $sql="update bypal_cust_zhimascore SET zmscore_mark='0',update_time='$time' where cust_id='".$cust_id."' and zmscore_num='".$num."'";
        $Model= new \Think\Model();
        $values=$Model->execute($sql);
        return $values;
    }

    public function delete_zmxy_rule($cust_id,$num){
        $time=time();
        $sql="update bypal_cust_zhimarule SET zmrule_mark='0',update_time='$time' where cust_id='".$cust_id."' and zmrule_num='".$num."'";
        $Model= new \Think\Model();
        $values=$Model->execute($sql);
        return $values;
    }

    public function insert_cust_vpn($cust_id,$relation_path,$state){
        $sql="insert into bypal_cust_vpn(cust_id,vpn_annex,create_time,state) values('$cust_id','$relation_path',now(),$state)";
        $Model= new \Think\Model();
        $values=$Model->execute($sql);
        return $values;
    }

}